Page({

  /**
   * 页面的初始数据
   */
  data: {
    id1:"back",
    id2:"clear",
    id3:"negative",
    id4:"+",
    id5:"1",
    id6:"2",
    id7:"3",
    id8:"-",
    id9:"4",
    id10:"5",
    id11:"6",
    id12:"×",
    id13:"7",
    id14:"8",
    id15:"9",
    id16:"÷",
    id17:"0",
    id18:".",
    id19:"history",
    id20:"=",
    resultData:"0",
    isOperator:false,
    lastChar:"0",
    lastOptIndex:0,
    currentValue:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 操作 组装运算公式
   */
  clickBtn:function(event){
    var me=this;
    var idData= event.target.id;
    var currentData= me.data;
    var resultData=currentData.resultData;
    var data;
    var isOperator=false;
    me.getLastOptIndex(currentData);
    var lastOptIndex=currentData.lastOptIndex;
    //如果是正负号
    if(idData==currentData.id3){
      if(lastOptIndex==0){
        var cZero=resultData.charAt(0);
        if(cZero=="-"){
          data= resultData.substring(1,resultData.length);
        }else{
          if(cZero=="0"){
            data=resultData;
          }else{
             data="-"+resultData;
           }
        }
      }else{
       var lastNum= resultData.substring(lastOptIndex+1,resultData.length);
       if(lastNum.charAt(0)=="("){
        lastNum= lastNum.substring(2,lastNum.length-1);
        data=resultData.substring(0,lastOptIndex+1)+lastNum;
      }else{
        if(lastNum==""){
          data=resultData;
          isOperator=true;
        }else {
          //找出前面的字符
          var charI=resultData.charAt(lastOptIndex);
          if(charI=="-"){
           data= resultData.substring(0,lastOptIndex)+"+"+lastNum;
          }else if(charI=="+"){
            data= resultData.substring(0,lastOptIndex)+"-"+lastNum;
          }else{
            data=resultData.substring(0,lastOptIndex+1)+"(-"+lastNum+")";
          }
        }
      }
     }
      //如果输入的是加减乘除
    }else if(idData==currentData.id4||idData==currentData.id8||idData==currentData.id12||idData==currentData.id16){
      if(currentData.isOperator){
        data = resultData; 
      }else{
        data = resultData+idData;
      }
      isOperator=true;
    }else{
      //如果resultData为0
      if (resultData == "0") {
        if (idData == currentData.id18){
          data = resultData+idData;
        }else{
          data = idData;
        }
      }else{
        //如果输入的是点
        if(idData==currentData.id18){
          //判断是否有运算符
          //找出运算符的位置，存放到数组
           //截取最后一个运算符所在的位置到结果字符串的末尾的字
           //然后判断此字符串中是否有“.”
            //如果最后一个字符串是运算符
        if(currentData.isOperator){
          data = resultData +"0"+ idData;  
        }else{
         var isPonit=false;
         var lastNum= resultData.substring(lastOptIndex+1,resultData.length);
          for(var i=0;i<lastNum.length;i++){
             var copt=lastNum.charAt(i);
             if(copt==currentData.id18){
                isPonit=true;
                break;
             }
           }
           //如果不存在那么直接加上
            if(isPonit){
              data = resultData ; 
            }else{
              data = resultData + idData; 
            }
          }
          //如果输入的是“0”
        }else if(currentData.id17==idData){
         var lastNum= resultData.substring(lastOptIndex+1,resultData.length);
           //如果不存在那么直接加上
            if(lastNum=="0"){
              data = resultData ; 
            }else{
              data = resultData + idData; 
            }
        }else {
          var lastNum= resultData.substring(lastOptIndex+1,resultData.length);
          if(lastNum=="0"){
             data=resultData.substring(0,resultData.length-1)+idData;
          }else if(lastNum.charAt(lastNum.length-1)==")"){
             data=resultData.substring(0,resultData.length-1)+idData+")";
          }
          else{
            data = resultData + idData; 
          }
        }
      }
    }
    me.setData({isOperator:isOperator});
    me.setData({resultData:data});
    me.setData({lastChar:idData});
  },
  getLastOptIndex:function(currentData){
    var resultData=currentData.resultData;
    var lastOptIndex=0;
    for(var i=0;i<resultData.length;i++){
         var copt=resultData.charAt(i);
          if(copt==currentData.id4||copt==currentData.id12||copt==currentData.id16){
                lastOptIndex=i;
          }else if(copt==currentData.id8){
               if(resultData.charAt(i-1)!="("){
                 lastOptIndex=i;
               }
          }
    }
     this.setData({lastOptIndex:lastOptIndex})
  },
  /**
   * 退格
   */
  clickBack:function(event){
    var me=this;
    var currentData=me.data;
    var dataStr=currentData.resultData;
    var data="0";
    if(dataStr!="0"){
       data=dataStr.substring(0,dataStr.length-1);
       if(data==""){
             data="0";
       }
      var lastChar=data.charAt(data.length-1);
      if(lastChar==currentData.id4||lastChar==currentData.id8||lastChar==currentData.id12||lastChar==currentData.id16){
        this.setData({isOperator:true});
      }else{
        this.setData({isOperator:false});
      }
    }
    this.setData({resultData:data});
  },

  /**
   * 清除显示
   */
  clickClear:function(event){
       this.setData({
         isOperator:false,
         resultData:"0",
         lastChar:"0"
       })
  },

/**
 * 求等于
 */
  clickEq:function(event){
    var me=this;
    var newStr=me.data.resultData.replace("÷","/").replace("×","*");
    console.log(newStr);
 
  },
  evl:function(){
    var me=this;
    var currentData=me.data;
    var resultData=currentData.resultData;
    var numArr=[];
    var optArr=[];
    var numStr="";
    var isStart=false;
    var lastChar=resultData.charAt(resultData.length-1);
    if(lastChar==currentData.id4||lastChar==currentData.id8||lastChar==currentData.id12||lastChar==currentData.id16){

    }else{
     for(var i=0;i<resultData.length;i++){
      var cChart= resultData.charAt(i);
       if(cChart==currentData.id4){
          optArr.push(cChart);
          isStart=true;
       }else if(cChart==currentData.id12){
          optArr.push("*");
          isStart=true;
       }else if(cChart==currentData.id16){
          optArr.push("/");
          isStart=true;
       }else if(cChart==currentData.id8){
          if(resultData.charAt(i-1)!="("){
             optArr.push("-");
             isStart=true;
          }else{
             numStr=numStr+cChart;
          }
       }else{
         if(i==resultData.length-1){
           numStr=numStr+cChart;
           numArr.push(numStr);
         }else{
          if(isStart){
          numArr.push(numStr);
          numStr=cChart;
          isStart=false;
         } else{
           numStr=numStr+cChart;
           isStart=false;
         }
        }
      }
     }
    }
    var gsStr="";
    for(var i=0;i<numArr.length;i++){
      var ns= numArr[i];
      if(ns.charAt(0)=="("){
        ns=ns.substr(1,ns.length-2);
        var figure=Number(ns);
        gsStr=gsStr+"("+figure+")";
      }else{
        var figure=Number(ns);
        gsStr=gsStr+figure;
      }
      if(i!=numArr.length-1){
        gsStr=gsStr+optArr[i];
      }
    }
    gsStr;
  },
  /**
   * 获取计算的历史
   */
  clickHistory:function(event){
    
  }

})